﻿using LMM_Editor.Classes.Entities;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace LMM_Editor.Classes.System
{
    public class FileManager
    {


        public static Image LoadImage(string path)
        {
            try
            {
                Image img = Image.FromFile(path);
                return img;
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("Unable to load file!");
                return null;
            }
        }

        public static void saveFile(string p)
        {
            writeLesson(p);
        }

        private static void writeLesson(string p)
        {
            using (XmlWriter w = XmlWriter.Create(p))
            {
                w.WriteStartDocument();
                w.WriteStartElement("System");
                w.WriteAttributeString("version", "1.0");

                writeEntities(w);
                w.WriteEndElement();
            }
        }

        private static void writeEntities(XmlWriter w)
        {
            foreach(IEntity entity in EntityManager.entities)
            {
                w.WriteStartElement("entity");

                /* Write basic attributes */
                w.WriteStartElement("basicproperties");
                w.WriteAttributeString("x", entity.component.Location.X.ToString());
                w.WriteAttributeString("y", entity.component.Location.Y.ToString());
                w.WriteAttributeString("weight", entity.component.Location.X.ToString());
                w.WriteAttributeString("height", entity.component.Location.Y.ToString());
                w.WriteAttributeString("text", entity.component.Text);
                w.WriteAttributeString("bgcolor", entity.component.BackColor.R+","+entity.component.BackColor.G+","+entity.component.BackColor.B+","+entity.component.BackColor.A);
                w.WriteEndElement();

                w.WriteEndElement();
            }
        }
    }
}
